package com.jandan.persistence.sqlmapdao;

import java.sql.SQLException;

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.jandan.persistence.iface.WordLibDao;
import com.jandan.ui.model.WordLib;

/**
 * WordLibDao接口实现类，使用Ibatis框架进行持久化
 * @author Gong Yong
 * @version 1.0
 * @see com.jandan.persistence.iface.WordLibDao
 */
public class WordLibSqlMapDao extends SqlMapClientDaoSupport implements WordLibDao {
	
	public WordLib getWordLibByWordLibID(long wordLibID) {
		// TODO Auto-generated method stub
		return (WordLib)this.getSqlMapClientTemplate().queryForObject("WordLib.getWordLibByWordLibID", Long.valueOf(wordLibID));
	}
	
	public List<WordLib> getAllUserWordLibList() {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("WordLib.getAllUserWordLibList");
	}
	
	public List<WordLib> getAllUserWordLibList(int start, int limit) {
		// TODO Auto-generated method stub
		return this.getSqlMapClientTemplate().queryForList("WordLib.getAllUserWordLibList",start,limit);
	}

	
	public long insertWordLib(WordLib wordLib) {
		// TODO Auto-generated method stub
		long wordLibID = 0;
		try {
			this.getSqlMapClient().startTransaction();
			this.getSqlMapClientTemplate().insert("WordLib.insertWordLib",wordLib);
			wordLibID = (Long)getSqlMapClientTemplate().queryForObject("WordLib.lastInsertWordLibID");
			this.getSqlMapClient().commitTransaction();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return wordLibID;
	}

	
	public void updateWordLib(WordLib wordLib) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().update("WordLib.updateWordLib", wordLib);
	}

	
	public void deleteWordLib(long wordLibID) {
		// TODO Auto-generated method stub
		this.getSqlMapClientTemplate().delete("WordLib.deleteWordLib",Long.valueOf(wordLibID));
	}


	public int getTotalWordLibCount() {
		// TODO Auto-generated method stub
		return (Integer)this.getSqlMapClientTemplate().queryForObject("WordLib.getTotalWordLibCount");
	}
}
